package cn.xzc.job.type.interval;

import cn.xzc.job.pojo.Job;

import java.util.Objects;

/**
 * 周期时间间隔执行的任务类型
 *
 * @author xzc
 * 当前时间 2024-02-12 21:30:00
 */
public class IntervalJob extends Job {

    /**
     * 任务执行的时间间隔
     */
    private final long interval;

    /**
     * 任务初始执行的延迟时间
     */
    private final long initialDelay;


    /**
     * Create a new {@code IntervalJob}.
     *
     * @param runnable     the underlying task to execute 要执行的具体任务
     * @param interval     how often in milliseconds the task should be executed 任务执行的时间间隔
     * @param initialDelay the initial delay before first execution of the task 任务初始执行的延迟时间
     */
    public IntervalJob(Runnable runnable, long interval, long initialDelay) {
        super(runnable);
        this.interval = interval;
        this.initialDelay = initialDelay;
    }

    /**
     * Create a new {@code IntervalJob} with no initial delay. 创建一个没有初始延迟的有间隔的任务类型对象
     *
     * @param runnable the underlying task to execute 要执行的具体任务
     * @param interval how often in milliseconds the task should be executed 任务执行的时间间隔
     */
    public IntervalJob(Runnable runnable, long interval) {
        this(runnable, interval, 0);
    }


    /**
     * Return how often in milliseconds the task should be executed.
     * 任务执行的时间间隔
     */
    public long getInterval() {
        return this.interval;
    }

    /**
     * Return the initial delay before first execution of the task.
     * 任务初始执行的延迟时间
     */
    public long getInitialDelay() {
        return this.initialDelay;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof IntervalJob)) {
            return false;
        }
        if (!super.equals(o)) {
            return false;
        }
        IntervalJob that = (IntervalJob) o;
        return interval == that.interval && initialDelay == that.initialDelay;
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), interval, initialDelay);
    }
}